using Front._1201;

namespace Front._1124;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        
    }
    private string currentUsername;

    public MainForm(string username)
    {
        InitializeComponent();
        currentUsername = username;
        InitializeMenu();
        this.Text = $"主窗体 - 欢迎 {username}";
    }

    private void InitializeMenu()
    {
        // 创建主菜单栏
        MenuStrip mainMenu = new MenuStrip();
        
        // 1. 文件菜单
        ToolStripMenuItem fileMenu = new ToolStripMenuItem("文件(&F)");
        
        var newItem = new ToolStripMenuItem("新建(&N)", null, OnNewFile, Keys.Control | Keys.N);
        var openItem = new ToolStripMenuItem("打开(&O)", null, OnOpenFile, Keys.Control | Keys.O);
        var saveItem = new ToolStripMenuItem("保存(&S)", null, OnSaveFile, Keys.Control | Keys.S);
        var exitItem = new ToolStripMenuItem("退出(&X)", null, OnExit);
        
        fileMenu.DropDownItems.AddRange(new ToolStripItem[] { newItem, openItem, saveItem, new ToolStripSeparator(), exitItem });

        // 2. 编辑菜单
        ToolStripMenuItem editMenu = new ToolStripMenuItem("编辑(&E)");
        
        var undoItem = new ToolStripMenuItem("撤销(&U)", null, OnUndo, Keys.Control | Keys.Z);
        var redoItem = new ToolStripMenuItem("重做(&R)", null, OnRedo, Keys.Control | Keys.Y);
        var cutItem = new ToolStripMenuItem("剪切(&T)", null, OnCut, Keys.Control | Keys.X);
        var copyItem = new ToolStripMenuItem("复制(&C)", null, OnCopy, Keys.Control | Keys.C);
        var pasteItem = new ToolStripMenuItem("粘贴(&P)", null, OnPaste, Keys.Control | Keys.V);
        
        editMenu.DropDownItems.AddRange(new ToolStripItem[] { undoItem, redoItem, new ToolStripSeparator(), cutItem, copyItem, pasteItem });

        // 3. 视图菜单
        ToolStripMenuItem viewMenu = new ToolStripMenuItem("视图(&V)");
        
        var toolbarItem = new ToolStripMenuItem("工具栏", null, OnToggleToolbar) { Checked = true };
        var statusbarItem = new ToolStripMenuItem("状态栏", null, OnToggleStatusbar) { Checked = true };
        var refreshItem = new ToolStripMenuItem("刷新(&R)", null, OnRefresh, Keys.F5);
        
        viewMenu.DropDownItems.AddRange(new ToolStripItem[] { toolbarItem, statusbarItem, new ToolStripSeparator(), refreshItem });

        // 4. 工具菜单
        ToolStripMenuItem toolsMenu = new ToolStripMenuItem("工具(&T)");
        
        var optionsItem = new ToolStripMenuItem("选项(&O)", null, OnOptions);
        var calculatorItem = new ToolStripMenuItem("计算器", null, OnCalculator);
        var notepadItem = new ToolStripMenuItem("记事本", null, OnNotepad);
        
        toolsMenu.DropDownItems.AddRange(new ToolStripItem[] { optionsItem, new ToolStripSeparator(), calculatorItem, notepadItem });

        // 5. 数据菜单
        ToolStripMenuItem dataMenu = new ToolStripMenuItem("数据(&D)");
        
        var importItem = new ToolStripMenuItem("导入数据", null, OnImportData);
        var exportItem = new ToolStripMenuItem("导出数据", null, OnExportData);
        var reportItem = new ToolStripMenuItem("生成报表", null, OnGenerateReport);
        
        dataMenu.DropDownItems.AddRange(new ToolStripItem[] { importItem, exportItem, new ToolStripSeparator(), reportItem });

        // 6. 帮助菜单
        ToolStripMenuItem helpMenu = new ToolStripMenuItem("帮助(&H)");
        
        var aboutItem = new ToolStripMenuItem("关于(&A)", null, OnAbout);
        var helpItem = new ToolStripMenuItem("帮助文档(&H)", null, OnHelp, Keys.F1);
        
        helpMenu.DropDownItems.AddRange(new ToolStripItem[] { helpItem, new ToolStripSeparator(), aboutItem });

        // 将菜单添加到主窗体
        mainMenu.Items.AddRange(new[] { fileMenu, editMenu, viewMenu, toolsMenu, dataMenu, helpMenu });
        this.MainMenuStrip = mainMenu;
        this.Controls.Add(mainMenu);
    }

    // 菜单事件处理方法
    private void OnNewFile(object sender, EventArgs e)
    {
        MessageBox.Show("新建文件功能");
    }

    private void OnOpenFile(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show($"打开文件: {dialog.FileName}");
        }
    }

    private void OnSaveFile(object sender, EventArgs e)
    {
        MessageBox.Show("保存文件功能");
    }

    private void OnExit(object sender, EventArgs e)
    {
        if (MessageBox.Show("确定要退出吗？", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            Application.Exit();
        }
    }

    private void OnUndo(object sender, EventArgs e) => MessageBox.Show("撤销操作");
    private void OnRedo(object sender, EventArgs e) => MessageBox.Show("重做操作");
    private void OnCut(object sender, EventArgs e) => MessageBox.Show("剪切操作");
    private void OnCopy(object sender, EventArgs e) => MessageBox.Show("复制操作");
    private void OnPaste(object sender, EventArgs e) => MessageBox.Show("粘贴操作");

    private void OnToggleToolbar(object sender, EventArgs e)
    {
        var item = sender as ToolStripMenuItem;
        item.Checked = !item.Checked;
        MessageBox.Show($"工具栏: {(item.Checked ? "显示" : "隐藏")}");
    }

    private void OnToggleStatusbar(object sender, EventArgs e)
    {
        var item = sender as ToolStripMenuItem;
        item.Checked = !item.Checked;
        MessageBox.Show($"状态栏: {(item.Checked ? "显示" : "隐藏")}");
    }

    private void OnRefresh(object sender, EventArgs e) => MessageBox.Show("刷新界面");

    private void OnOptions(object sender, EventArgs e)
    {
        MessageBox.Show("系统选项设置");
    }

    private void OnCalculator(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start("calc.exe");
    }

    private void OnNotepad(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start("notepad.exe");
    }

    private void OnImportData(object sender, EventArgs e) => MessageBox.Show("导入数据");
    private void OnExportData(object sender, EventArgs e) => MessageBox.Show("导出数据");
    private void OnGenerateReport(object sender, EventArgs e) => MessageBox.Show("生成报表");

    private void OnAbout(object sender, EventArgs e)
    {
        MessageBox.Show($"应用程序 v1.0\n当前用户: {currentUsername}", "关于");
    }

    private void OnHelp(object sender, EventArgs e)
    {
        MessageBox.Show("打开帮助文档");
    }
    private void toolStripMenuItem_BookMgr_Click(object sender, EventArgs e)
    {
        BookMain bookMain = new BookMain();
        bookMain.ShowDialog();
    }
}